package file.day9_20.day05;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket serverSocket;

    public Server(){
        try{
            System.out.println("正在启动服务端...");

            serverSocket = new ServerSocket(8088); //使用ServerSocket类创建一个8088的服务端接口
            System.out.println("服务器启动完毕");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public void start(){
        try{

            System.out.println("等待客户端连接");

            Socket socket = serverSocket.accept();//截停，接收到有客户端使用端口继续执行，将收到的Socket类对象赋值给socket
            //收到socket可以进行各种读写操作

            System.out.println("一个客户端连接了！！！");

            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(),"UTF-8"//指定socket块读入
                    )
            );

            //读取socket块里的信息
            String message = null;
            while ((message = br.readLine()) != null){
                System.out.println("客户端说："+message);
            }

        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
